#include "sdlManager.h"

//The color of the font
SDL_Color textColor = {255,255,255};

//check for having piece
bool hasPiece = false;
//animation through frames
SDL_Rect frameBR[20];
SDL_Rect frameDR[20];
SDL_Rect frameDW[20];
SDL_Rect frameEL[20];
SDL_Rect frameKN[20];
SDL_Rect frameMA[20];
SDL_Rect frameSC[20];
SDL_Rect frameSL[20];
SDL_Rect frameFL[20];
SDL_Rect frameYE[20];
SDL_Rect frameSO[20];
SDL_Rect frameEX[20];

int onBoard;
static int ArryPos;

int calculateDistance(int x1, int x2, int y1, int y2)
{
    double distance;

    distance = sqrt( pow( (double)x2 - (double)x1, 2 ) + pow( (double)y2 - (double)y1, 2 ) );
    sqrt(distance);

    return (int)distance;
}

void sdlManager::setGrid()
{
    int xStart = 0; int yStart = 0;
    for(int y = 0; y < 8; ++y)
    {
        for(int x = 0;x < 10; ++x)
        {
            //this is for the two forbiden zones on the grid
            if(x == 2 && y == 3 || x == 3 && y == 3 || x == 2 && y == 4 || x == 3 && y == 4
                || x == 6 && y == 3 || x == 6 && y == 4 || x == 7 && y == 3 || x == 7 && y == 4)
            {
                board[y][x].occupied = true;
            }

            else
            board[y][x].occupied = false;
            board[y][x].location.Ypos = yStart;
            board[y][x].location.Xpos = xStart;
            xStart += 70;
        }
        yStart += 75;
        xStart = 0;
    }
}
void sdlManager::pieceSelection(SDL_Surface *screen)
{   
    //for boundaries of movement
    static int availableMovL;
    static int availableMovR;
    static int availableMovU;
    static int availableMovD;

    if(buttonDownL)
    {
        availableMovL = 0;////this
        availableMovR = 0;////is
        availableMovD = 0;////all i
        availableMovU = 0;////added! I think! lol

        if(hasPiece)
            hasPiece = false;
        for(int i = 0; i < pieces.size(); ++i)
        {
            if(board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos == pieces.at(i).location.Xpos
                && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos == pieces.at(i).location.Ypos)
            {
                cout << "Piece is " << pieces.at(i).piece << endl;
                hasPiece = true;
                ArryPos = i;
            }
        }
    }
    if(hasPiece)
    {
//checks if it could move in a direction
       
        for(int i = 0; i < 4; i++)
            for(int j = 0; j < pieces.at(ArryPos).moves; j++)
            {
                //x left
                if(i == 0)
                {
                   
                    if(pieces.at(ArryPos).location.Xpos/70 == 0)
                    {
                        break;
                    }
                    if(!board[(pieces.at(ArryPos).location.Ypos/75)][(pieces.at(ArryPos).location.Xpos/70)-j-1].occupied)
                    {
                        apply_surface(board[(pieces.at(ArryPos).location.Ypos/75)][(pieces.at(ArryPos).location.Xpos/70)-j-1].location.Xpos,
                            board[(pieces.at(ArryPos).location.Ypos/75)][(pieces.at(ArryPos).location.Xpos/70)-j-1].location.Ypos,
                            MoveMarker,screen,NULL);
                        availableMovL = j + 1;
                        if(pieces.at(ArryPos).piece == 9)
                        {
                            j = pieces.at(ArryPos).moves;
                            break;
                        }
                    }
                    else if(pieces.at(ArryPos).piece == 1)
                    {
                        j = pieces.at(ArryPos).moves;
                        break;
                    }
                    if((pieces.at(ArryPos).location.Xpos/70)-j-1 == 0)
                    {
                        j = pieces.at(ArryPos).moves;
                    }
                   
                }
                //x right
                if(i == 1)
                {
                    //if pieces is at the edge there is no need to draw
                    if(pieces.at(ArryPos).location.Xpos/70 == 9)
                    {
                        break;
                    }

                    if(!board[(pieces.at(ArryPos).location.Ypos/75)][(pieces.at(ArryPos).location.Xpos/70)+j+1].occupied)
                    {
                        apply_surface(board[(pieces.at(ArryPos).location.Ypos/75)][(pieces.at(ArryPos).location.Xpos/70)+j+1].location.Xpos,
                            board[(pieces.at(ArryPos).location.Ypos/75)][(pieces.at(ArryPos).location.Xpos/70)+j+1].location.Ypos,
                            MoveMarker,screen,NULL);
                        availableMovR = j + 1;
                        if(pieces.at(ArryPos).piece == 9)
                        {
                       
                            j = pieces.at(ArryPos).moves;
                           
                            break;
                        }
                    }
                    //if it equals scout
                    else if(pieces.at(ArryPos).piece == 1)
                    {
                        j = pieces.at(ArryPos).moves;
                        break;
                    }
                    if((pieces.at(ArryPos).location.Xpos/70)+j+1 == 9)
                    {
                        j = pieces.at(ArryPos).moves;
                    }
                   
                }
                //y up
                if(i == 2)
                {
                   
                    //checks for up edge for drawing
                    if(pieces.at(ArryPos).location.Ypos/75 == 0)
                    {
                        break;
                    }

                    if(!board[(pieces.at(ArryPos).location.Ypos/75)-j-1][(pieces.at(ArryPos).location.Xpos/70)].occupied)
                    {
                        apply_surface(board[(pieces.at(ArryPos).location.Ypos/75)- j - 1][(pieces.at(ArryPos).location.Xpos/70)].location.Xpos,
                            board[(pieces.at(ArryPos).location.Ypos/75)- j - 1][(pieces.at(ArryPos).location.Xpos/70)].location.Ypos,
                            MoveMarker,screen,NULL);
                        availableMovU = j + 1;
                        if(pieces.at(ArryPos).piece == 9)
                        {
                            j = pieces.at(ArryPos).moves;
                            break;
                        }

                    }
                    else if(pieces.at(ArryPos).piece == 1)
                    {
                        j = pieces.at(ArryPos).moves;
                        break;
                    }
                    if((pieces.at(ArryPos).location.Ypos/75)-j-1 == 0)
                    {
                        j = pieces.at(ArryPos).moves;
                    }
                   
                }
                //y down
                if(i == 3)
                {   
                   
                    if(pieces.at(ArryPos).location.Ypos/75 == 7)
                    {
                        break;
                    }
                    if(!board[(pieces.at(ArryPos).location.Ypos/75)+j+1][(pieces.at(ArryPos).location.Xpos/70)].occupied)
                    {
                        apply_surface(board[(pieces.at(ArryPos).location.Ypos/75)+j+1][(pieces.at(ArryPos).location.Xpos/70)].location.Xpos,
                            board[(pieces.at(ArryPos).location.Ypos/75)+j+1][(pieces.at(ArryPos).location.Xpos/70)].location.Ypos,
                            MoveMarker,screen,NULL);
                        availableMovD = j + 1;
                        if(pieces.at(ArryPos).piece == 9)
                        {
                            j = pieces.at(ArryPos).moves;
                            break;
                        }
                    }
                    //for scout
                    else if(pieces.at(ArryPos).piece == 1)
                    {
                        j = pieces.at(ArryPos).moves;
                        break;
                    }
                    if((pieces.at(ArryPos).location.Ypos/75)+j+1 == 7)
                    {
                        j = pieces.at(ArryPos).moves;
                    }
                }
            }
			availableMovL *= -1;
			availableMovU *= -1;
            /*cout << "left: " << availableMovL << endl;
            cout << "down: " << availableMovD << endl;
            cout << "up: " << availableMovU << endl;
            cout << "right: " << availableMovR << endl;*/
        }
        if(buttonDownR && hasPiece)
            {
				int distanceX,distanceY;

				distanceY = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos/70 - pieces.at(ArryPos).location.Xpos/70;
				distanceX = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos/75 - pieces.at(ArryPos).location.Ypos/75;

                // if clicked within boundaries
                if(distanceX < 0 || distanceY < 0)
                {
					cout << "left or up" << endl;
					cout << "Left: " << availableMovL << "Distance: " << distanceY << endl;
					cout << "Up: " << availableMovU << "Distance: " << distanceX << endl;
					if((distanceY == 0 && (distanceX >= availableMovU)) || 
						(distanceX == 0 && (distanceY >= availableMovL)) )
					{
						if(!board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied )
						{
							board[pieces.at(ArryPos).location.Ypos/75][pieces.at(ArryPos).location.Xpos/70].occupied = false;
							board[pieces.at(ArryPos).location.Ypos/75][pieces.at(ArryPos).location.Xpos/70].revealed = false;
							///
								pieces.at(ArryPos).movingLeft = true;
								pieces.at(ArryPos).movingRight = false;
							///
							pieces.at(ArryPos).location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
							pieces.at(ArryPos).location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
							//
							board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
							board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
							hasPiece = false;
						}
					}
                }
				else if(distanceX > 0 || distanceY > 0)
				{
					cout << "right or down" << endl;
					cout << "Down: " << availableMovD << "Distance: " << distanceX<< endl;
					cout << "Right: " << availableMovR << "Distance: " << distanceY << endl;
					if((distanceY == 0 && (distanceX <= availableMovD)) || 
						(distanceX == 0 && (distanceY <= availableMovR)) )
					{
						if(!board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied )
						{
							board[pieces.at(ArryPos).location.Ypos/75][pieces.at(ArryPos).location.Xpos/70].occupied = false;
							board[pieces.at(ArryPos).location.Ypos/75][pieces.at(ArryPos).location.Xpos/70].revealed = false;
							///
								pieces.at(ArryPos).movingLeft = false;
								pieces.at(ArryPos).movingRight = true;
							///
							pieces.at(ArryPos).location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
							pieces.at(ArryPos).location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
							//
							board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
							board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
							hasPiece = false;
						}
					}
				}
            }
        buttonDownR = false;
        buttonDownL = false;
}

void sdlManager::piecePlacement(SDL_Surface *screen)
{
    //to access and push back on vectors
    BoardGame b;
    //temp variable to store peice value
    static int pieceNum = 0;
    //counters for tracking
    static int beastRiders = 0;
    static int dragons = 0;
    static int dwarfs = 0;
    static int elfs = 0;
    static int flags = 0;
    static int knights = 0;
    static int mages = 0;
    static int scouts = 0;
    static int slayers2 = 0;
    static int yetis = 0;
    static int traps = 0;
    static int sorcerers = 0;
   

    //draws pieces on board for selection
    for(int i = 0; i < 12; i++)
    {
        settingPieces[i].location.Xpos = 750;
        settingPieces[i].location.Ypos = i * 55;
       
        if(i == 0 && slayers2 < MAX_SLAYERS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, slayer2, screen, &frameSL[0]);
        else if(i == 1 && scouts < MAX_SCOUTS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, scout, screen, &frameSC[0]);
        else if(i == 2 && dwarfs < MAX_DWARFS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, dwarf, screen, &frameDW[0]);
        else if(i == 3 && elfs < MAX_ELFS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, elf, screen, &frameEL[0]);
        else if(i == 4 && yetis < MAX_YETIS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, yeti, screen, &frameYE[0]);
        else if(i == 5 && sorcerers < MAX_SORCERERS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, sorcerer, screen, &frameSO[0]);
        else if(i == 6 && beastRiders < MAX_BEASTRIDERS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, beastRider, screen, &frameBR[0]);
        else if(i == 7 && knights < MAX_KNIGHTS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, knight, screen, &frameKN[0]);
        else if(i == 8 && mages < MAX_MAGES)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, mage, screen, &frameMA[0]);
        else if(i == 9 && dragons < MAX_DRAGONS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, dragon, screen, &frameDR[0]);
        else if(i == 10 && flags < MAX_FLAGS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, flag, screen, &frameFL[0]);
        else if(i == 11 && traps < MAX_TRAPS)
            apply_surface(settingPieces[i].location.Xpos,settingPieces[i].location.Ypos, trap, screen, NULL);

    }

    if(buttonDownL == true)
    {
        //slayer
        if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos <  800 && mouseXYClick.Ypos <  55)
        {
            pieceNum = 0;
            buttonDownL = false;
            //cout << b.SLAYER << endl;
   
        }
        //scout
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 55 && mouseXYClick.Ypos < 105)
        {
            pieceNum = 1;
            buttonDownL = false;

        }
        //dwarf
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 105 && mouseXYClick.Ypos < 160)
        {
            pieceNum = 2;
            buttonDownL = false;
        }

        //elf
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXY.Ypos > 160 && mouseXYClick.Ypos < 215)
        {
            pieceNum = 3;
            buttonDownL = false;
        }
        //yeti
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 215 && mouseXYClick.Ypos < 270)
        {
            pieceNum = 4;
            buttonDownL = false;
        }
        //sorcerer
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 270 && mouseXYClick.Ypos < 325)
        {
            pieceNum = 5;
            buttonDownL = false;
        }
        //beastRider
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 325 && mouseXYClick.Ypos < 380)
        {
            pieceNum = 6;
            buttonDownL = false;
        }

        //knight
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 380 && mouseXYClick.Ypos < 425)
        {
            pieceNum = 7;
            buttonDownL = false;
        }
        //mage
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 425 && mouseXYClick.Ypos < 480)
        {
            pieceNum = 8;
            buttonDownL = false;
        }
        //dragon
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 480 && mouseXYClick.Ypos < 525)
        {
            pieceNum = 9;
            buttonDownL = false;
        }
        //flag
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 525 && mouseXYClick.Ypos < 580)
        {
            pieceNum = 10;
            buttonDownL = false;
        }
        else if( mouseXYClick.Xpos > 750 && mouseXYClick.Xpos < 800 && mouseXYClick.Ypos > 580 && mouseXYClick.Ypos < 625)
        {
            pieceNum = 11;
            buttonDownL = false;
        }
       
    }


    //this if where you place the pieces on the board
    if(mouseInBounds() == true && buttonDownR == true)
    {
        int xStart = 0; int yStart = 375;
        for(int y = 5; y < 8; ++y)
        {
        for(int x = 0;x < 10; ++x)
            {
                switch(pieceNum)
                {
                case b.SLAYER:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && slayers2 < MAX_SLAYERS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        slayers2 += 1;
                        onBoard++;
                        }
                    break;
                case b.SCOUT:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && scouts < MAX_SCOUTS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 9;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        scouts += 1;
                        onBoard++;
                        }
                    break;
                case b.DWARF:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && dwarfs < MAX_DWARFS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        dwarfs += 1;
                        onBoard++;
                        }
                    break;
                case b.ELF:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && elfs < MAX_ELFS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        elfs += 1;
                        onBoard++;
                        }                   
                    break;
                case b.YETI:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && yetis < MAX_YETIS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = true;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        yetis += 1;
                        onBoard++;
                        }
                    break;
                case b.SORCERER:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && sorcerers < MAX_SORCERERS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        sorcerers += 1;
                        onBoard++;
                        }
                    break;
                case b.BEASTRIDER:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && beastRiders < MAX_BEASTRIDERS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        beastRiders += 1;
                        onBoard++;
                        }
                    break;
                case b.KNIGHT:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && knights < MAX_KNIGHTS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        knights += 1;
                        onBoard++;
                        }
                    break;
                case b.MAGE:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && mages < MAX_MAGES)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 1;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        mages += 1;
                        onBoard++;
                        }
                    break;
                case b.DRAGON:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && dragons < MAX_DRAGONS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 9;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        dragons += 1;
                        onBoard++;
                        }
                    break;
                case b.FLAG:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && flags < MAX_FLAGS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingLeft = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 0;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        flags += 1;
                        onBoard++;
                        }
                    break;
                case b.TRAP:
                    if(mouseXYClick.Xpos > xStart && mouseXYClick.Xpos < xStart + 75 && mouseXYClick.Ypos > yStart && mouseXY.Ypos < yStart + 75
                        && board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied == false && traps < MAX_TRAPS)
                        {
                        b.piece = pieceNum;
                        b.location.Xpos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Xpos;
                        b.location.Ypos = board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].location.Ypos;
                        b.frameCounter = 0;
                        //temporary to check
                        b.movingRight = false;
                        ////////////////////
                        b.movingRight = false;
                        b.attackLeft = false;
                        b.attackRight = false;
                        b.moves = 0;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].occupied = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].revealed = false;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].playerSide = true;
                        board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].piece = pieceNum;
                        //board[mouseXYClick.Ypos/75][mouseXYClick.Xpos/70].moves = 2;
                        pieces.push_back(b);
                        traps += 1;
                        onBoard++;
                        }
                    break;
                default:
                    break;
                }
                if(onBoard >= 30)
                {
                    settingBoard = false;
                }
               
                xStart += 70;
            }
            yStart += 75;
            xStart = 0;
        }
   
    }
    buttonDownL = false;
    buttonDownR = false;
}



